package com.yiren.dbaa.module.practice_set.line.presenter;

import android.app.Activity;
import android.content.DialogInterface;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.line.contract.AddLineContract;
import com.yiren.dbaa.module.practice_set.line.contract.LineHomeContract;
import com.yiren.dbaa.module.practice_set.line.model.AddLineModel;
import com.yiren.dbaa.module.practice_set.line.model.LineHomeModel;
import com.yiren.dbaa.module.practice_set.site.bean.LineDataValBean;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/15 18:24
 * @description: 新增线路
 */
public class AddLinePresenter implements AddLineContract.Presenter {
    private Activity activity;
    private AddLineContract.Model addLineModel;
    private LineHomeContract.Model lineHomeModel;
    private UserParamConfigBean userParamConfig;

    public AddLinePresenter(Activity activity) {
        this.activity = activity;
        addLineModel = new AddLineModel(activity);
        lineHomeModel = new LineHomeModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadShareLine(AddLineContract.View view,String name,String user) {
        addLineModel.getShareLine(userParamConfig.getSite().getSiteCode(),name,user, new CommonCallBack() {
            @Override
            public void success(Object result) {
                view.showShareLine((List<LineInfoEntity>) result);
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void downLoadLine(LineInfoEntity lineInfo,List<LineInfoEntity> lineListSer, CommonCallBack callBack) {
        boolean isHaveData = addLineModel.isHaveData(lineInfo,lineListSer);
        int pUserID = lineInfo.getUserId();
        int cUserID = userParamConfig.getUserInfo().getId();
        if (isHaveData) {
            CommonDialog.showTwoBtn(activity, "操作确认", "您已存在" + lineInfo.getName() + "，下载将会覆盖!\n确定覆盖下载吗?", "取消", "覆盖下载", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        lineHomeModel.downLoadLinePointData(lineInfo.getName(), lineInfo.getLineVer(), lineInfo.getSiteCode(), pUserID, cUserID, new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                //本地数据插入
                                LineDataValBean lineDataValBean = (LineDataValBean) result;
                                lineInfo.setPartneModifyVal(lineDataValBean.getPartnerLineData());
                                lineInfo.setCoachModifyVal(lineDataValBean.getCoachLineData());

                                if (GradeDbController.getInstance(false).insertOrReplace(lineInfo)) {
                                    //通知视图
                                    callBack.success("");
                                    MessageBox.MessageAndVoiceModel(activity, "下载成功");

                                } else {
                                    MessageBox.MessageAndVoiceModel(activity, "下载失败");
                                }
                            }

                            @Override
                            public void fail(String error) {
                                MessageBox.MessageAndVoiceModel(activity, "下载失败!");
                            }
                        });
                        dialog.dismiss();
                    }
                }
            });
        } else {
            lineHomeModel.downLoadLinePointData(lineInfo.getName(), lineInfo.getLineVer(), lineInfo.getSiteCode(), pUserID, cUserID, new CommonCallBack() {
                @Override
                public void success(Object result) {
                    //本地数据插入
                    LineDataValBean lineDataValBean = (LineDataValBean) result;
                    lineInfo.setPartneModifyVal(lineDataValBean.getPartnerLineData());
                    lineInfo.setCoachModifyVal(lineDataValBean.getCoachLineData());
                    if (GradeDbController.getInstance(false).insertOrReplace(lineInfo)) {
                        //通知视图
                        callBack.success("");
                        MessageBox.MessageAndVoiceModel(activity, "下载成功");
                    } else {
                        MessageBox.MessageAndVoiceModel(activity, "下载失败");
                    }
                }

                @Override
                public void fail(String error) {
                    MessageBox.MessageAndVoiceModel(activity, "下载失败!");
                }
            });
        }
    }

}